package com.deyang.ui.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.deyang.adapter.MailAdapter;
import com.deyang.entity.MailModel;
import com.deyang.entity.OrganizationModel;
import com.deyang.maillist.R;
import com.deyang.util.DialogUtil;
import com.deyang.util.ViewUtil;
import com.deyang.widget.TopBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MailListActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar topBar;
    @BindView(R.id.tv_noData)
    TextView tvNoData;
    @BindView(R.id.rv_mailList)
    RecyclerView rvMailList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    private int currentPage = 1, maxPage = 5;
    private int lastSize = 0;

    private List<MailModel> mailModelList = new ArrayList<>();
    private MailAdapter mailAdapter;

    private String[] names = {"张顺谷", "张悌斯", "张灶冲", "易江维", "孙来笙", "饶展林", "岳列洋", "时党舒", "周迟蒲", "廉梦容"};
    private String[] dutys = {"安保员", "app试玩员", "按摩师", "编辑", "保洁员", "保姆", "保育员", "办公室文员", "标准化工程师", "播音员"};
    private String[] sexs = {"1", "1", "1", "0", "0", "1", "1", "0", "1", "0"};
    private String[] phones = {"13000001111", "13000002222", "13000003333", "13400001111", "13500001111", "17000001111", "13400001111", "13500001111", "13123001111", "13005711111",};


    @Override
    protected void initView(Bundle savedInstanceState) {
        //状态栏透明化
        setContentView(R.layout.activity_mail_list);
        ButterKnife.bind(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String organizationName = intent.getStringExtra("organizationName");
        String organizationNumber = intent.getStringExtra("organizationNumber");

        int number = Integer.parseInt(organizationNumber);
        maxPage = number / 10 + 1;
        lastSize = number % 10;

        topBar.setCenterText(organizationName);

        mailAdapter = new MailAdapter(this, mailModelList);
        ViewUtil.initRecycleView(this, false, rvMailList, mailAdapter);
        loadData(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @Override
    public void setListener() {
        super.setListener();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData(false);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadData(true);
            }
        });

        mailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MailModel mailModel = mailModelList.get(position);
                Intent intent = new Intent(MailListActivity.this, MailDetailActivity.class);
                intent.putExtra("mailName",mailModel.getName());
                intent.putExtra("mailPhone",mailModel.getPhone());
                intent.putExtra("mailDuty",mailModel.getDuty());
                intent.putExtra("mailSex",mailModel.getSex());
                startActivity(intent);
            }
        });
    }


    /**
     * 加载数据
     *
     * @param isLoadMore
     */
    private void loadData(final boolean isLoadMore) {
        if (!isLoadMore) {
            currentPage = 1;
        }
        boolean isLastPage = false;
        if (currentPage > maxPage) {
            showToastError("无更多数据");
            refreshLayout.finishLoadMore();
            return;
        } else if (currentPage == maxPage) {
            isLastPage = true;
        }
        Dialog loadingDialog = DialogUtil.getLoadingDialog(this);
        loadingDialog.show();

        List<MailModel> newMailModelList = new ArrayList<>();

        int size = isLastPage ? lastSize : 10;
        for (int i = 0; i < size; i++) {
            newMailModelList.add(new MailModel(names[i], dutys[i], sexs[i], phones[i]));
        }

        if (!isLoadMore) {
            mailModelList.clear();
            mailAdapter.setNewData(newMailModelList);
            if (newMailModelList.size() == 0) {
                tvNoData.setVisibility(View.VISIBLE);
            } else {
                tvNoData.setVisibility(View.GONE);
            }
        } else {
            mailAdapter.addData(newMailModelList);
        }
        mailModelList.addAll(newMailModelList);
        if (isLoadMore) {
            refreshLayout.finishLoadMore();
        } else {
            refreshLayout.finishRefresh();
        }
        loadingDialog.dismiss();

        currentPage++;
    }

}
